/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <postgresql/config.h>
#include <postgresql/postgresql_core.h>
#include <postgresql/postgresql_comm.h>
#include "../test_postgresql_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __postgresql_parse_bind(Conntrack *p_st_conn, L7session_postgresql_private *p_st_private, int i_packet_len);

static void __case1(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x42\x00\x00\x00\x32\x00\x00\x00\x02\x00\x00\x00\x00\x00\x02\x00\x00\x00\x06\xe5\xb0\x8f\xe5\x88\x98\x00\x00\x00\x14\x32\x30\x31\x36\x2d\x30\x31\x2d\x30\x34\x20\x2b\x30\x38\x3a\x30\x30\x3a\x30\x30\x00\x00",
                 51);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;

    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case2(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x42\x00\x00\x00\x32\x00\x00\x00\x02\x00\x00\x00\x00\x00\x02\x00\x00\x00\x06\xe5\xb0\x8f\xe5\x88\x98\x00\x00\x00\x14\x32\x30\x31\x36\x2d\x30\x31\x2d\x30\x34\x20\x2b\x30\x38\x3a\x30\x30\x3a\x30\x30\x00\x00",
                 51);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));

    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case3(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data, (char *) "\x42\x00\x00\x00\x04", 5);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;

    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case4(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data, (char *) "\x42\x00\x00\x00\x07\x44\x52\x00", 8);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;
    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case5(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data, (char *) "\x42\x00\x00\x00\x0a\x44\x52\x00\x44\x52\x00", 11);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;
    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case6(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x42\x00\x00\x00\x0F\x00\x00\x00\x02\x00\x00\x00\x00\x00\x03\x00", 16);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;
    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case7(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x42\x00\x00\x00\x2F\x00\x00\x00\x02\x00\x00\x00\x00\x00\x02\x00\x00\x00\x06\xe5\xb0\x8f\xe5\x88\x98\x00\x00\x00\x14\x32\x30\x31\x36\x2d\x30\x31\x2d\x30\x34\x20\x2b\x30\x38\x3a\x30\x30\x3a\x30",
                 48);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;
    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

static void __case8(void)
{
    int i_ret = FR_SUCC;
    int i_packet_len = 0;

    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_postgresql_create_ct(&p_st_conn, TEST_POSTGRESQL_SRC_IP, TEST_POSTGRESQL_DST_IP, TEST_POSTGRESQL_SRC_PORT, TEST_POSTGRESQL_DST_PORT), FR_SUCC);
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_postgresql_private *p_st_private = (L7session_postgresql_private *) frct_get_session_private(p_st_session);

    p_st_private->p_st_fragment->p_st_data = bytebuff_creat();
    bytebuff_add(p_st_private->p_st_fragment->p_st_data,
                 (char *) "\x42\x00\x00\x00\x31\x00\x00\x00\x02\x00\x00\x00\x00\x00\x02\x00\x00\x00\x06\xe5\xb0\x8f\xe5\x88\x98\x00\x00\x00\x14\x32\x30\x31\x36\x2d\x30\x31\x2d\x30\x34\x20\x2b\x30\x38\x3a\x30\x30\x3a\x30\x00",
                 50);
    bytebuff_skip(p_st_private->p_st_fragment->p_st_data, 1);
    postgresql_get_int2(p_st_private->p_st_fragment->p_st_data, &i_packet_len);
    i_packet_len += 1;
    p_st_private->p_st_req = (L7_req_postgresql *) frmalloc(sizeof(L7_req_postgresql));
    memset(p_st_private->p_st_req, 0x0, sizeof(L7_req_postgresql));
    Postgresql_req_cmd *p_st_command = NULL;
    Frlist_node *p_st_node = NULL;
    Sql_statement *p_st_statement = NULL;

    /* create statement */
    p_st_statement = frdb_create_statement();
    /* 创建一个command信息结构体 */
    p_st_command = (Postgresql_req_cmd *) frmalloc(sizeof(Postgresql_req_cmd));
    memset(p_st_command, 0x0, sizeof(Postgresql_req_cmd));
    p_st_command->i_new_flg = FR_YES;
    p_st_command->p_st_statement = p_st_statement;
    p_st_node = frlist_node_create(p_st_command);
    frlist_push(&p_st_private->p_st_req->st_command_lst, p_st_node);
    p_st_private->p_st_req->p_st_command_pre = p_st_command;
    i_ret = __postgresql_parse_bind(NULL, p_st_private, i_packet_len);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    //CU_ASSERT_STRING_EQUAL(p_user, "postgres");
    //CU_ASSERT_STRING_EQUAL(p_database, "postgres");

    test_postgresql_destroy_ct(&p_st_conn);

    return;
}

int add_suite__postgresql_parse_bind(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __postgresql_parse_bind()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL ||
        CU_add_test(pSuite, "case8", __case8) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
